<meta charset="utf-8">
<div id="container"></div>
<pre id="console" style="visibility: hidden;"></pre>
<script>
    function log(message)
    {
        document.getElementById("console").appendChild(document.createTextNode(message + "\n"));
    }

    function testFindString(text, target, options, expectedRanges)
    {
        log("Searching for \u2018" + target + "\u2019 " + (text.length <= 64 ? "in \u2018" + text + "\u2019 " : "in long string ") + "with options [" + options.join(", ") + "]:");

        var container = document.getElementById("container");
        container.innerHTML = text;
        document.body.offsetTop;
        var selection = getSelection();
        selection.empty();

        var expectedRange;
        while (expectedRange = expectedRanges.shift()) {
            var found = testRunner.findString(target, options);
            if (found) {
                var actualRange = [selection.baseOffset, selection.extentOffset];
                if (expectedRange[0] !== actualRange[0] || expectedRange[1] !== actualRange[1])
                    log("FAIL: Expected a match at " + expectedRange + " but got a match at " + actualRange + " instead.");
                else
                    log("PASS: Got a match at " + expectedRange + " as expected.");
            } else if (expectedRange.length)
                log("FAIL: Expected " + expectedRange + " but got no match.");
            else
                log("PASS: Got no match as expected.");
        }
        container.innerText = "";
        log("");
    }

    testRunner.dumpAsText();

    testFindString("Lorem ipsum dolor sit amet", "o", [], [[1, 2], [13, 14], [15, 16], []]);
    testFindString("Lorem ipsum dolor sit amet", "o", ["WrapAround"], [[1, 2], [13, 14], [15, 16], [1, 2]]);
    testFindString("Lorem ipsum dolor sit amet", "o", ["Backwards"], [[15, 16], [13, 14], [1, 2], []]);
    testFindString("Lorem ipsum dolor sit amet", "o", ["Backwards", "WrapAround"], [[15, 16], [13, 14], [1, 2], [15, 16]]);
    testFindString("Lorem ipsum dolor sit amet", "O", [], [[]]);
    testFindString("Lorem ipsum dolor sit amet", "O", ["CaseInsensitive"], [[1, 2], [13, 14], [15, 16]]);

    testFindString("insurmountable mountain", "mount", [], [[5, 10], [15, 20], []]);

    testFindString("cocoa", "co", [], [[0, 2], [2, 4], []]);

    testFindString("webkit.org", "org", [], [[7, 10]]);
    testFindString("webkit.org", ".org", [], [[6, 10], []]);

    testFindString("\u8d77\u52d5\u6226\u58eb", "\u52d5\u6226\u58eb", [], [[1, 4], []]);
    testFindString("\u8d77\u52d5\u6226\u58eb", "\u6226\u58eb", [], [[2, 4], []]);
    testFindString("\u8d77\u52d5\u6226\u58eb", "\u58eb", [], [[3, 4], []]);

    const searchBufferSize = 8192;
    const searchBufferOverlapSize = searchBufferSize / 4;
    const searchBufferUnoverlappedSize = searchBufferSize - searchBufferOverlapSize;
    var bufferSizedString = "X";
    while (bufferSizedString.length < searchBufferSize)
        bufferSizedString += bufferSizedString;
    bufferSizedString = bufferSizedString.substring(0, searchBufferSize);

    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize - 2) + " ba a" + bufferSizedString, "a", [],
                   [[searchBufferUnoverlappedSize, searchBufferUnoverlappedSize+1], [searchBufferUnoverlappedSize + 2, searchBufferUnoverlappedSize + 3], []]);

    var thaiWords = [
        "\u0e01\u0e23",
        "\u0e1b\u0e39\u0e40\u0e25",
        "\u0e01\u0e0a",
        "\u0e01\u0e0a\u0e01\u0e23", // thaiWords[2] + thaiWords[0]
        "\u0e01\u0e23\u0e01\u0e0a", // thaiWords[0] + thaiWords[2]
        "\u0e1a\u0e07\u0e01\u0e0a", // ends with thaiWords[2]
    ];

    testFindString(thaiWords.join(""), thaiWords[0], [], [[0, 2], [10, 12], [12, 14], []]);

    testFindString(thaiWords.join(""), thaiWords[2], [], [[6, 8], [8, 10], [14, 16], [18, 20], []]);

    testFindString(bufferSizedString.substring(0, searchBufferUnoverlappedSize) + thaiWords.join("") + bufferSizedString, thaiWords[0], [], [[searchBufferUnoverlappedSize, searchBufferUnoverlappedSize + 2], [searchBufferUnoverlappedSize + 10, searchBufferUnoverlappedSize + 12], [searchBufferUnoverlappedSize + 12, searchBufferUnoverlappedSize + 14], []]);
  
    testFindString("Spaces, the final frontier", " ", [], [[7, 8], [11, 12], [17, 18], []]);
    testFindString("Use an @import rule", "@", [], [[7, 8], []]);
    testFindString("If ((x + 5) * 2) = 14, then x = 2", "(x", [], [[4, 6], []]);

    testFindString("hello<img src='../resources/abe.png'>world", "lowo", [], [[]]);

    document.getElementById("console").style.removeProperty("visibility");
</script>
